package SoftwareEngineering01.WordFrequence.Step4;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class UniteVerb {
    public static String uniteVerb(String str, File file) throws IOException{
        String [] list = str.split("\\W+");
        Map<String,String> map = new HashMap<>();
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String string = "";
        while((string = bufferedReader.readLine()) != null){
            string = string.trim();
            String [] verbs = string.split(" ");
            for (int i = 0; i < verbs.length; i++) {
                map.put(verbs[i],verbs[0]);
            }
        }
        bufferedReader.close();
        inputStreamReader.close();
        fileInputStream.close();

        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < list.length; i++) {
            if(map.containsKey(list[i])){
                stringBuffer.append(map.get(list[i])+" ");
            }else{
                stringBuffer.append(list[i]+" ");
            }
        }
        String s = stringBuffer.toString();
        return s;
    }
}
